package fr.xebia.monitoring.beans.formater;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import fr.xebia.monitoring.beans.AggregatedMeasure;
import fr.xebia.monitoring.beans.AggregatedMeasures;
import fr.xebia.monitoring.beans.Measure;

public class MeasureCsvFormater implements MeasureFormater {

	public MeasureCsvFormater() {
		super();
	}

	public String format(AggregatedMeasures agregatedMeasures) throws FormaterException {
		StringWriter stringWriter = new StringWriter();
		format(agregatedMeasures, stringWriter);
		return stringWriter.toString();
	}

	public void format(AggregatedMeasures agregatedMeasures, Writer writer) throws FormaterException {
		try {
			StringBuilder buffer = new StringBuilder("CategoryName;TotalTime;AverageTime;MaxTime;MinTime;HitCount;\n");
			writer.write(buffer.toString());
			for (AggregatedMeasure agregatedMeasure : agregatedMeasures.getAggregatedMeasure()) {
				buffer.delete(0, buffer.length());
				formatMeasure(buffer, agregatedMeasure);
				buffer.append("\n");
				writer.write(buffer.toString());
			}
		} catch (IOException e) {
			throw new FormaterException("Can't generate CSV output", e);
		}
	}

	public String format(AggregatedMeasure agregatedMeasure) {
		StringBuilder buffer = new StringBuilder();
		formatMeasure(buffer, agregatedMeasure);
		return buffer.toString();
	}

	private void formatMeasure(StringBuilder buffer, AggregatedMeasure agregatedMeasure) {
		buffer.append(agregatedMeasure.getCategoryName()).append(";");
		buffer.append(String.valueOf(agregatedMeasure.getTotalTime())).append(";");
		buffer.append(String.valueOf(agregatedMeasure.getAverageTime())).append(";");
		buffer.append(String.valueOf(agregatedMeasure.getMaxTime())).append(";");
		buffer.append(String.valueOf(agregatedMeasure.getMinTime())).append(";");
		buffer.append(String.valueOf(agregatedMeasure.getHitCount())).append(";");
	}

	public void format(Measure[] measures, Writer writer) {
		try {
			StringBuilder buffer = new StringBuilder("CategoryName;ElapsedTime");
			writer.write(buffer.toString());
			for (Measure measure : measures) {
				buffer.delete(0, buffer.length());
				buffer.append(measure.getCategoryName()).append(";");
				buffer.append(String.valueOf(measure.getElapsedTime()));
				writer.write(buffer.toString());
			}
		} catch (IOException e) {
			throw new FormaterException("Can't generate CSV output", e);
		}
	}

	public String format(Measure[] measures) {
		StringBuilder buffer = new StringBuilder("CategoryName;ElapsedTime");
		for (Measure measure : measures) {
			buffer.append(measure.getCategoryName()).append(";");
			buffer.append(String.valueOf(measure.getElapsedTime()));
		}
		return buffer.toString();
	}

	public String format(Measure measure) {
		StringBuilder buffer = new StringBuilder();
		buffer.append(measure.getCategoryName()).append(";");
		buffer.append(String.valueOf(measure.getElapsedTime()));
		return buffer.toString();
	}
}
